﻿using Newtonsoft.Json;
using Refit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace CPlatePrintSys.Infrastructure.Web {
    [Obsolete]
    public interface IWebCommunication {
        // 用户登录
        [Post("/api/login")]
        Task<LoginResp> Login(Dictionary<string, object> req, [Header("Authorization")] string token, CancellationToken cancellationToken = default);
        
        // 用户登出
        [Post("/api/login/logout")]
        Task<LogoutResp> Logout([Header("Authorization")] string token, CancellationToken cancellationToken = default);
        
        // 同步加工序列
        [Post("/system/processing-sequences/sync")]
        Task<ProcessSequenceCommonResp> ProcessSeqSync([Body(BodySerializationMethod.UrlEncoded)] ProcessSequenceSyncReq req, [Header("Authorization")] string token, CancellationToken cancellationToken = default);
       
        // 添加加工序列
        [Post("/system/processing-sequences")]
        Task<ProcessSequenceEditResp> ProcessSeqAdd([Body(BodySerializationMethod.UrlEncoded)] ProcessSequenceModel req, [Header("Authorization")] string token, CancellationToken cancellationToken = default);
        
        // 修改加工序列
        [Put("/system/processing-sequences")]
        Task<ProcessSequenceCommonResp> ProcessSeqUpdate([Body(BodySerializationMethod.UrlEncoded)] ProcessSequenceModel req, [Header("Authorization")] string token, CancellationToken cancellationToken = default);
        
        // 删除加工序列
        [Delete("/system/processing-sequences")]
        Task<ProcessSequenceCommonResp> ProcessSeqDelete([Body(BodySerializationMethod.UrlEncoded)] LoginReq req, [Header("Authorization")] string token, CancellationToken cancellationToken = default);


        // 删除加工序列
        [Delete("/system/processing-sequences")]
        Task<ProcessSequenceCommonResp> QueryProcessSeq([Body(BodySerializationMethod.UrlEncoded)] QueryProcessSeqReq req, [Header("Authorization")] string token, CancellationToken cancellationToken = default);
        // 删除加工序列
        [Delete("/system/processing-sequences")]
        Task<ProcessSequenceCommonResp> QueryDrawing([Body(BodySerializationMethod.UrlEncoded)] LoginReq req, [Header("Authorization")] string token, CancellationToken cancellationToken = default);
        // 删除加工序列
        [Delete("/system/processing-sequences")]
        Task<ProcessSequenceCommonResp> ExportDrawing([Body(BodySerializationMethod.UrlEncoded)] LoginReq req, [Header("Authorization")] string token, CancellationToken cancellationToken = default);
        // 删除加工序列
        [Delete("/system/processing-sequences")]
        Task<ProcessSequenceCommonResp> QueryProcessSeqDetails([Body(BodySerializationMethod.UrlEncoded)] LoginReq req, [Header("Authorization")] string token, CancellationToken cancellationToken = default);
        // 删除加工序列
        [Delete("/system/processing-sequences")]
        Task<ProcessSequenceCommonResp> GetShips([Body(BodySerializationMethod.UrlEncoded)] Dictionary<string, object> req, [Header("Authorization")] string token, CancellationToken cancellationToken = default);
        // 删除加工序列
        [Delete("/system/processing-sequences")]
        Task<ProcessSequenceCommonResp> GetUnits([Body(BodySerializationMethod.UrlEncoded)] Dictionary<string, object> req, [Header("Authorization")] string token, CancellationToken cancellationToken = default);
        // 删除加工序列
        [Delete("/system/processing-sequences")]
        Task<ProcessSequenceCommonResp> GetLOT([Body(BodySerializationMethod.UrlEncoded)] Dictionary<string, object> req, [Header("Authorization")] string token, CancellationToken cancellationToken = default);
        // 删除加工序列
        [Delete("/system/processing-sequences")]
        Task<ProcessSequenceCommonResp> GetProcessSeqs([Body(BodySerializationMethod.UrlEncoded)] Dictionary<string, object> req, [Header("Authorization")] string token, CancellationToken cancellationToken = default);

    }

    public class WebCommunication {
        private string baseUrl = string.Empty;
        private string token = string.Empty;
        
        public WebCommunication(string baseUrl, string token) {
            this.baseUrl = baseUrl;
            this.token = $"Bearer {token}";           
        }

        public Tuple<LoginResp, bool> Login(LoginReq param) {            
            var api = RestService.For<IWebCommunication>(this.baseUrl);            
            var cts = new CancellationTokenSource(TimeSpan.FromSeconds(50)); // 10-second timeout
            try {
                Dictionary<string, object> req = new Dictionary<string, object>
                {
                    ["username"] = "user123",
                    ["password"] = "password123",
                    ["captcha_id"] = Guid.NewGuid().ToString(),
                    ["captcha_code"] = "ABCD"
                };
                LoginResp resp = api.Login(req, this.token, cts.Token).Result;
                return new Tuple<LoginResp, bool>(resp, true);
            } catch (TaskCanceledException) {
                Console.WriteLine("Request canceled or timed out.");
            }

            return new Tuple<LoginResp, bool>(null, false);
        }

        public Tuple<LogoutResp, bool> Logout() {
            var api = RestService.For<IWebCommunication>(this.baseUrl);
            var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)); // 10-second timeout
            try {
                LogoutResp resp = api.Logout(this.token, cts.Token).Result;
                return new Tuple<LogoutResp, bool>(resp, true);
            } catch (TaskCanceledException) {
                Console.WriteLine("Request canceled or timed out.");
            }

            return new Tuple<LogoutResp, bool>(null, false);
        }
    }

}
